package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.EditText;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.data.cache.db.DataBaseManager;
import com.zw.yzk.kabu.data.cache.db.User;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2017/3/4.
 */

public class EditNickNameActivity extends BaseActivity {

    @Bind(R.id.nick_name)
    EditText editNickName;

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_nick_name;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.edit_nick_name))
                .setRightText(getResources().getString(R.string.save))
                .build();
    }

    @Override
    public void initComponent() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        User user = DataBaseManager.getInstance().getLoginUser();
        if(user != null) {
            editNickName.setText(user.getNickName());
        }
    }

    @OnClick(R.id.right_text)
    public void saveNickName() {
        String nickName = editNickName.getText().toString();
        if (TextUtils.isEmpty(nickName)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.nickname_can_not_be_empty));
            return;
        } else if(nickName.length() > 10) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.nickname_do_not_much_limit));
            return;
        }
        Intent intent = new Intent();
        intent.putExtra(ActivityConstant.EDIT_NICKNAME, nickName);
        setResult(RESULT_OK, intent);
        finish();
    }
}
